package cn.edu.gsau.info.controller;

import cn.edu.gsau.info.models.ResponseResult;
import cn.edu.gsau.info.models.Zhengmingcailiao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @BelongsProject: gsau
 * @BelongsPackage: cn.edu.gsau.info.controller
 * @Author: zwh
 * @CreateTime: 2022-11-23  14:46
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/upload")
public class JinsaiUpload {

    @Value("${gsau.upload.path}")
    private String uploadFile;

    @PostMapping("/save")
    public ResponseResult upload(MultipartFile file){

        File path=new File(uploadFile+"/7");
        if(path.exists()==false){
            path.mkdirs();
        }



        //文件名
        String originalFilename = file.getOriginalFilename();
        //文件后缀
        String suffix=originalFilename.substring(originalFilename.lastIndexOf("."));

        //new fileName
        String fileName= "7/"+UUID.randomUUID().toString()+suffix;
        System.out.println(fileName);

        //存储文件
        try {
            file.transferTo(new File(uploadFile,fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Zhengmingcailiao zhengmingcailiao=new Zhengmingcailiao();
        zhengmingcailiao.setFileName(originalFilename);
        zhengmingcailiao.setFileFullPath(fileName);
        zhengmingcailiao.setModuleId(7);
        return new ResponseResult(200,"保存成功",zhengmingcailiao);
    }

}
